﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Xml;
using System.Security.Cryptography;
using System.Configuration;

namespace Com.CMS263.Util
{
    public static class SendMessageUtil
    {

        public static string userId = ConfigurationManager.AppSettings["emsUserId"];
        public static string userName = ConfigurationManager.AppSettings["emsUserPass"];

        /// <summary>
        /// Post 数据至接口端函数
        /// </summary>
        /// <param name="postContent">提交内容</param>
        /// <param name="url">提交地址</param>
        /// <returns></returns>
        public static string PostHttp(string postContent, string url)
        {
            string resultstr = string.Empty;
            System.Text.Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "text/xml";
            req.Timeout = 1000000;
            Byte[] bytes = encode.GetBytes(postContent);
            req.ContentLength = bytes.Length;
            Stream sendStream = req.GetRequestStream();
            sendStream.Write(bytes, 0, bytes.Length);
            sendStream.Close();
            System.Net.WebResponse rep = req.GetResponse();
            Stream getStream = rep.GetResponseStream();
            using (StreamReader sr = new StreamReader(getStream, Encoding.UTF8))
            {
                resultstr = sr.ReadToEnd();
                sr.Close();
            }
            getStream.Close();
            rep.Close();
            return resultstr;
        }

        ///发送短信方法
        public static string sendSms(string telephone,string contents,int id)
        {
            //构建所需Post的参数            
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"<Group Login_Name=""{0}"" Login_Pwd=""{1}"" OpKind=""0"" InterFaceID=""2"">", "zimituan", EncryptTomd5("zimituan"));
            sb.AppendFormat("<E_Time>{0}</E_Time>", "");
            sb.AppendFormat("<Item>");
            sb.AppendFormat("<Task><Recive_Phone_Number>{0}</Recive_Phone_Number>", telephone);
            sb.AppendFormat("<Content><![CDATA[{0}]]></Content>", contents);
            sb.AppendFormat("<Search_ID>{0}</Search_ID>", id.ToString());
            sb.Append("</Task>");
            sb.Append("</Item></Group>");

            return PostHttp(sb.ToString(), "http://slapi.vcomcn.com/Opration.aspx");
        }       
        //用MD5方式加密字符串函数
        public static string EncryptTomd5(string strPassword)
        {
            ASCIIEncoding objAsc = new ASCIIEncoding();
            MD5 objMD5 = new MD5CryptoServiceProvider();
            byte[] arrRndHashPwd = objMD5.ComputeHash(objAsc.GetBytes(strPassword));
            string strRndHashPwd = "";
            foreach (byte b in arrRndHashPwd)
            {
                if (b < 16)
                    strRndHashPwd = strRndHashPwd + "0" + b.ToString("X");
                else
                    strRndHashPwd = strRndHashPwd + b.ToString("X");
            }
            return strRndHashPwd;
        }



        /// <summary>
        /// 把文件存成base64编码的字节串函数
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string FormatToBase64(string FilePath)
        {
            string Base64str = string.Empty;
            using (FileStream Fs = new FileStream(FilePath, FileMode.Open))
            {
                byte[] B = new byte[Fs.Length];
                Fs.Read(B, 0, (int)Fs.Length);
                Base64str = Convert.ToBase64String(B);
                Fs.Close();
            }
            return Base64str;
        }




        ///按时间段方式的结果查询方法
        public static DataTable SearchEmsByTime()
        {
            DataTable Dt = new DataTable();

            //构建所需Post的参数            
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"<Root Login_Name=""{0}"" Login_Pwd=""{1}"" Service_Type=""7"" InterFaceID=""20070918"">", "userid", EncryptTomd5("userpwd"));
            sb.AppendFormat("<Start_Time>{0}</Start_Time>", "2007-9-24 22:00:00");
            sb.AppendFormat("<End_Time>{0}</End_Time>", "2008-01-01 22:00:00");
            sb.AppendFormat("<PageSize>{0}</PageSize>", 20);
            sb.AppendFormat("<PageIndex>{0}</PageIndex>", 3);
            sb.Append("</Root>");

            string returnxml = PostHttp(sb.ToString(), "http://userinterface.vcomcn.com/GetResult.aspx");
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(returnxml);
            foreach (XmlNode xnode in xdoc.SelectNodes("/Group/Result"))
            {
                DataRow dr = Dt.NewRow();
                dr["Mobile"] = xnode.SelectSingleNode("./Mobile").InnerText;
                dr["Message"] = xnode.SelectSingleNode("./Message").InnerText;
                dr["Sijian"] = xnode.SelectSingleNode("./Sijian").InnerText;
                Dt.Rows.Add(dr);
            }
            return Dt;
        }


        ///按查询ID方式的结果查询方法
        public static DataTable button4_Click()
        {
            DataTable Dt = new DataTable();
            //构建所需Post的参数            
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"<Root Login_Name=""{0}"" Login_Pwd=""{1}"" Service_Type=""1"" InterFaceID=""10"">", "username", EncryptTomd5("userpwd"));
            sb.Append("<Item>");
            sb.AppendFormat("<Search_ID>{0}</Search_ID>", "99999");
            sb.AppendFormat("<Search_ID>{0}</Search_ID>", "88888");
            sb.Append("</Item>");
            sb.Append("</Root>");

            string returnxml = PostHttp(sb.ToString(), "http://userinterface.vcomcn.com/GetResult.aspx");
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(returnxml);
            foreach (XmlNode xnode in xdoc.SelectNodes("/Group/Result"))
            {
                DataRow dr = Dt.NewRow();
                dr["Mobile"] = xnode.SelectSingleNode("./Mobile").InnerText;
                dr["Message"] = xnode.SelectSingleNode("./Message").InnerText;
                dr["Sijian"] = xnode.SelectSingleNode("./Sijian").InnerText;
                Dt.Rows.Add(dr);
            }
            return Dt;
        }

        public static string GetSmsBalance()
        {
            //构建所需Post的参数            
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"<Root  Service_Type=""0"">");
            sb.Append("<Item>");
            sb.AppendFormat("<Account_Name>{0}</Account_Name>", "zimituan");
            sb.AppendFormat("<Account_Name>zimituan</Account_Name>");
            sb.AppendFormat(@"<Account_Name>""zimituan""</Account_Name>");
            sb.Append("</Item>");
            sb.Append("</Root>");
            string returnxml = PostHttp(sb.ToString(), "http://userinterface.vcomcn.com/GetResult.aspx");
            return returnxml;
        }

        /////发送文本语音方法
        //private string button3_Click(object sender, EventArgs e)
        //{
        //    //构建所需Post的参数            
        //    StringBuilder sb = new StringBuilder();
        //    sb.AppendFormat(@"<Group Login_Name=""username"" Login_Pwd=""{0}"" OpKind=""2"" InterFaceID=""15"">", EncryptTomd5("userpwd"));
        //    sb.Append("<B_Time></B_Time>");
        //    sb.Append("<E_Time></E_Time>");
        //    sb.Append("<Fail_Retry_Times>3</Fail_Retry_Times>");
        //    sb.Append("<R_Interval>60</R_Interval>");
        //    sb.Append("<Display_Number>83078700</Display_Number>");
        //    sb.Append("<Voice_Action_Num>1</Voice_Action_Num>");
        //    sb.Append("<Link_Tel></Link_Tel>");
        //    sb.Append("<Item><Task><Tts_Content><![CDATA[祝''深圳发展银行''全体员工''节日''愉快！]]>");
        //    sb.Append("</Tts_Content>");
        //    sb.AppendFormat("<Recive_Phone_Number1>{0}</Recive_Phone_Number1><Recive_Phone_Number2>", "15880027202");
        //    sb.Append("</Recive_Phone_Number2>13959395432<Recive_Phone_Number3></Recive_Phone_Number3><Search_ID>123456</Search_ID></Task></Item></Group>");

        //    return PostHttp(sb.ToString(), "http://userinterface.vcomcn.com/Opration.aspx");
        //}


        /////发送组合语音方法
        //private void button9_Click(object sender, EventArgs e)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    sb.AppendFormat(@"<Group Login_Name=""{0}"" Login_Pwd=""{1}"" OpKind=""{2}"" InterFaceID=""66"">", "username", EncryptTomd5("userpwd"), "3");
        //    sb.AppendFormat("<B_Time>{0}</B_Time>", "");
        //    sb.AppendFormat("<E_Time>{0}</E_Time>", "");
        //    sb.Append("<Fail_Retry_Times>3</Fail_Retry_Times>");
        //    sb.Append("<R_Interval>120</R_Interval>");
        //    sb.Append("<Display_Number>888888</Display_Number>");
        //    sb.Append("<Link_Tel>013520097941</Link_Tel>");
        //    sb.Append("<Voice_File_Item>");
        //    sb.Append("<Voice_File>");
        //    sb.AppendFormat("<ext>{0}</ext>", Path.GetExtension(textBox1.Text));
        //    sb.Append("<match_num>2</match_num>");
        //    sb.AppendFormat(@"<BinaryValue dt:dt=""binary.base64"" xmlns:dt=""urn:schemas-microsoft-com:datatypes"">{0}</BinaryValue>", FormatToBase64(textBox1.Text));
        //    sb.Append("</Voice_File>");
        //    sb.Append("</Voice_File_Item>");
        //    sb.Append("<Voice_Action_Num>1</Voice_Action_Num>");
        //    sb.Append("<Item>");
        //    sb.Append("<Task>");
        //    sb.Append("<Tts_ConTent_Item>");
        //    sb.AppendFormat(@"<Tts_Content match_num=""{0}"">{1}</Tts_Content>", "1", "第五段人工语音");
        //    sb.Append("</Tts_ConTent_Item>");
        //    sb.Append("<Recive_Phone_Number1>01088465195-603</Recive_Phone_Number1>");
        //    sb.Append("<Recive_Phone_Number2></Recive_Phone_Number2>");
        //    sb.Append("<Recive_Phone_Number3></Recive_Phone_Number3>");
        //    sb.Append("<Search_ID>123321</Search_ID>");
        //    sb.Append("</Task>");
        //    sb.Append("</Item>");
        //    sb.Append("</Group>");

        //    return PostHttp(sb.toString(), "http://userinterface.vcomcn.com/Opration.aspx");

        //}



        
    }
}
